<template>
  <div class="app-main">
    <div class="main-header">
      <el-page-header @back="reset('ruleForm')" content="新增项目" />
      <div class="header-right">
        <el-button size="mini" @click="reset('ruleForm')">取 消</el-button>
        <el-button size="mini" type="primary" :loading="loading" @click="sure" >确 定</el-button>
      </div>
    </div>
    <div class="main-contain">
      <el-form :model="ruleForm" label-position="top" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <div class="contain-item">
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目名称" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目部署地址" prop="url">
                <el-input v-model="ruleForm.url"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
            <el-form-item label="项目描述" prop="desc">
              <el-input type="textarea" rows="3" v-model="ruleForm.desc" placeholder="请输入项目描述"></el-input>
            </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目类型" prop="type">
                <el-radio-group v-model="ruleForm.type">
                  <el-radio :label="1">个人项目</el-radio>
                  <el-radio :label="2">外包项目 </el-radio>
                  <el-radio :label="3">内部项目 </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目状态" prop="status">
                <el-radio-group v-model="ruleForm.status">
                  <el-radio :label="1">开始</el-radio>
                  <el-radio :label="2">暂停</el-radio>
                  <el-radio :label="3">关闭</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <div class="contain-item">
          <el-form-item label="项目技术">
            <div class="form-item-tag">
              <d-tag :dynamicTags.sync="dynamicTags" />
            </div>
          </el-form-item>
        </div>
        <el-form-item label="源代码存放地址">
          <el-upload
            class="upload-demo"
            ref="uploadBox"
            drag
            action="action"
            :before-upload="beforeUpload"
            :http-request="save"
            :auto-upload="false">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传 zip/rar/gz/apk 格式的文件</div>
          </el-upload>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
let formData = new FormData()
export default {
  name: 'addProject',
  data () {
    return {
      loading: false,
      ruleForm: {
        name: '',
        desc: '',
        type: 1,
        url: '',
        status: 1
      },
      rules: {
        name: [
          { required: true, message: '请输入项目名称', trigger: 'blur' }
        ]
      },
      dynamicTags: [],
    }
  },
  methods: {
    beforeUpload (file) { // 上传文件之前钩子
      let regFile = file.name.replace(/.+\./, '')
      let exe = ['zip', 'rar', 'gz', 'apk']
      if (exe.indexOf(regFile.toLowerCase()) !== -1) {
        formData.append('file', file)
      } else {
        this.$message.error('文件格式有误，请重新上传')
        return false
      }
    },
    sure () {
      this.$refs.uploadBox.submit()
    },
    reset(formName) {
      this.$refs[formName].resetFields()
      this.dynamicTags = []
      formData = new FormData()
      this.$router.go(-1)
    },
    save() { // 新增
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          formData.append('name', this.ruleForm.name)
          formData.append('type', this.ruleForm.type)
          formData.append('url', this.ruleForm.url)
          formData.append('status', this.ruleForm.status)
          formData.append('desc', this.ruleForm.desc)
          formData.append('skill', this.dynamicTags.join(','))
          formData.append('uid', this.$Cookies.get('uid'))
          this.$API({
            name: 'AddProject',
            data: formData,
            requireAuth: true
          }).then(res => {
            if (res.data.code === 200) {
              this.$notify.success(res.data.msg)
              formData = new FormData()
              this.$router.go(-1)
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(error => {
            this.$message.error(error)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-form-item {
  margin-bottom: 15px !important;
}
.app-main {
  .main-header {
    height: 31px;
    width: 100%;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .main-contain {
    padding-top: 10px;
    overflow-y: auto;
    width: 100%;
    flex: 1 1 auto;
    .contain-item {
      .el-form-item {
        padding: 0 5px;
        flex: 1;
      }
      .el-rate {
        border-radius: 4px;
        border: 1px solid #DCDFE6;
        padding: 0 15px
      }
      .form-item-tag {
        border-radius: 4px;
        border: 1px solid #DCDFE6;
        padding: 0 15px;
      }
    }
    .el-rate {
      height: 40px !important;
      line-height: 2.5 !important;
    }
  }
}
</style>